package mylibrary.reflect.sample;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test {

	public static void main(String[] args) throws Exception {
		
		Class demo = Class.forName("mylibrary.reflect.sample.Person");
		
		Constructor[] cons = demo.getConstructors();

		System.out.println(cons[0].getParameterTypes().length);
		System.out.println(cons[1].getParameterTypes().length);
		System.out.println(cons[2].getParameterTypes().length);
		
		//Person person1 = (Person) cons[0].newInstance();
		//Person person2 = (Person) cons[1].newInstance("gao");
		//Person person3 = (Person) cons[2].newInstance("gao", 10);

		
		Constructor cons0 = demo.getConstructor(String.class);
		Person person0 = (Person) cons0.newInstance("gao");
		
		Constructor cons1 = demo.getConstructor(String.class, int.class);
		Person person1 = (Person) cons1.newInstance("gao", 20);
		
		Constructor cons2 = demo.getConstructor();
		Person person2 = (Person) cons2.newInstance();
		person2.setName("shuai");
		
		System.out.println(person0);
		System.out.println(person1);
		System.out.println(person2);
		
		
		//security test
		Person per = new Person();
		Field field = per.getClass().getDeclaredField("name");
		//field.setAccessible(true);
		//field.set(per, "gaogaogoa");
		//System.out.println(per);
		
		Method method = per.getClass().getDeclaredMethod("test", String.class);
		method.setAccessible(true);
		Object result = method.invoke(per, "ss");
		System.out.println(result);
		
	}
			
}
